use std::fs::File;
use std::io::{self, Read};

fn read_username_from_file() -> Result<String, io::Error> 
{
    let mut username_file = File::open("hello.txt")?;
    let mut username = String::new();
    username_file.read_to_string(&mut username)?;
    Ok(username)
}

fn main() {
    let greeting_file_result = File::open("hello.txt");

    let mut greeting_file = match greeting_file_result {
        Ok(file) => file,
        Err(error) => File::create("hello.txt").unwrap(),
    };

    let mut greeting = String::new();
    match greeting_file.read_to_string(&mut greeting) {
        Ok(_) => println!("Hello, {}!", greeting),
        Err(error) => panic!("Problem reading the file: {error:?}"),
    }

    let username_result = read_username_from_file();
    match username_result {
        Ok(username) => println!("Welcome, {}!", username),
        Err(error) => println!("Error reading username: {error:?}"),
    }
    // panic!("This is a panic message");
}
